﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using User_subsystem;
using web_search;

namespace BookManageSystem
{
    public partial class FormUpdateBook : Form
    {
        public FormUpdateBook()
        {
            InitializeComponent();
        }

        private void button添加_Click(object sender, EventArgs e)
        {
            if (textISBN.Text.Trim() == "" ||     
                textTitle.Text.Trim() == "" ||
                textAuthor.Text.Trim() == "" ||
                textPrice.Text.Trim() == "" 
                )
            {
                MessageBox.Show("有空项", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            DaoAdmin dao = new DaoAdmin();
            dao.connect();
            string sql = $"update BookInfor set " +
                $"ISBN='{textISBN.Text}'," +
                $"BookID='{textBookID.Text}'," +
                $"BookName='{textTitle.Text}'," +
                $"AuthorName='{textAuthor.Text}'," +
                $"Price={float.Parse(textPrice.Text)}" +        
                $"where BookID='{FormBookInformationForAdministrator.select_bookid}'";
            if (dao.Execute(sql) > 0)
            {
                MessageBox.Show("更新成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("更新失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 取消_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormUpdateBook_Load(object sender, EventArgs e)
        {
            string bookid = FormBookInformationForAdministrator.select_bookid;
            
            DaoAdmin dao=new DaoAdmin();
            dao.connect();
            string sql = $"select * from V_UserBookInfor where BookID='{bookid}'";

            SqlDataReader reader = dao.read(sql);
            if (reader.Read())
            {
                textISBN.Text = reader[1].ToString();
                textBookID.Text = reader[0].ToString();
                textTitle.Text = reader[2].ToString();
                textAuthor.Text = reader[3].ToString();
                textPrice.Text = reader[5].ToString();
            }
            
        }
    }
}
